<?php
/**
 * @Notes:【】
 * @Date: 2022-06-03 0:42
 */

namespace Yj\Generator\Core\Dao\Template;

use Yj\Generator\Core\Common\RenderableTemplate;
use Yj\Generator\Core\Dao\Template\Traits\ClassNameTrait;
use Yj\Generator\Core\Dao\Template\Traits\DocBlockTrait;
use Yj\Generator\Core\Dao\Template\Traits\MethodTrait;
use Yj\Generator\Core\Dao\Template\Traits\NameSpaceTrait;
use Yj\Generator\Core\Dao\Template\Traits\UsesTrait;

class BaseTemplate extends RenderableTemplate
{
    use NameSpaceTrait;
    use ClassNameTrait;
    use UsesTrait;
    use DocBlockTrait;
    use MethodTrait;


    public function toLines()
    {
        $lines = [];
        $lines[] = $this->ln('<?php');

        if ($this->namespace !== null) {
            $lines[] = $this->ln($this->namespace->render());
        }

        if (count($this->uses) > 0) {
            $lines[] = $this->renderArrayLn($this->uses);
        }

        if ($this->docBlock !== null) {
            $lines[] = $this->docBlock->render();
        }

        $lines[] = $this->className?->render();

        $this->processMethods($lines);

        $lines[] = $this->ln('}');

        return $lines;
    }

    private function processMethods(array &$lines)
    {
        $methods = $this->getMethods();
        if (count($methods) > 0) {
            $lines[] = $this->renderArray($methods, 4, str_repeat(PHP_EOL, 2));
        }
    }

}